package com.maysahar;

import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.maysahar.model.Entry;

public class AddActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        View view =  findViewById(R.id.addButton);
        final Button button = (Button) findViewById(R.id.addButton);
        button.setOnClickListener(
        		new View.OnClickListener() {             
        			public void onClick(View v) {
        				addItemToDatabase();
        				// Perform action on click
        		    	Context context = getApplicationContext();
        		    	CharSequence text = "Added new item";
        		    	int duration = Toast.LENGTH_SHORT;
        		    	Toast toast = Toast.makeText(context, text, duration);
        		    	toast.show();
        		    	finish();
        			}         
        		}
        );
    }

    private void addItemToDatabase() {
        final TextView propNameTv = (TextView) findViewById(R.id.propertyName);
        final TextView propValueTv = (TextView) findViewById(R.id.propertyValue);
        String propName = propNameTv.getText().toString();
        String propValue = propValueTv.getText().toString();
        Entry entry = new Entry();
        entry.setName(propName);
        entry.setValue(propValue);
        entry.setSetting(0);
        entry.setVersion(0);
        entry.setModifiedOn(new Date());
        EntryDAO dao = new EntryDAO(this);
        dao.open();
    	dao.create(entry);
    	dao.close();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_add, menu);
        return true;
    }
}
